package com.example.gadgeteer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class LocationService extends Service
{
	
   @Override
   public void onCreate() {
   super.onCreate();
   Toast.makeText(this,"service created",Toast.LENGTH_LONG).show();
   LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   LocationListener ll = new mylocationlistener();
   //  minimum distance interval for notifications is 4 meters
   //the minimum time interval for notifications is 2 seconds
   lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 4, ll);    
   }
   
	@Override
	public void onDestroy() {
		super.onDestroy();
		Toast.makeText(this,"Service destroyed ...", Toast.LENGTH_SHORT).show();
	}
	
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}	
   private class mylocationlistener implements LocationListener {
   
      @Override
      public void onProviderDisabled(String provider) {
       Toast.makeText(LocationService.this,"Error onProviderDisabled",Toast.LENGTH_LONG).show();
      }    
      @Override
      public void onProviderEnabled(String provider) {
       Toast.makeText(LocationService.this,"onProviderEnabled",Toast.LENGTH_LONG).show();
      }
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
       Toast.makeText(LocationService.this,"onStatusChanged",Toast.LENGTH_LONG).show();
      }
		@Override
		public void onLocationChanged(Location location)
		{
		     if (location != null) {
		   //  Toast.makeText(LocationService.this,"locationChanged"+  location.getLatitude(),Toast.LENGTH_LONG).show();
		   //  Log.d("LOCATION CHANGED", location.getLatitude() + "");
		   //  Log.d("LOCATION CHANGED", location.getLongitude() + "");
		     //
		     HttpClient client = new DefaultHttpClient();
		     HttpPost post = new HttpPost("http://uclwebservicetest.cloudapp.net/Services/DataLogging.svc/logData");
		     try {
		   		 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		   	    Date date = new Date();
			       Calendar calendar = Calendar.getInstance();
			       calendar.setTime(date);
			       calendar.add(Calendar.HOUR, -1);
			       Date date2 = new Date(calendar.getTimeInMillis());
			       String time = sdf.format(date2); 
		   	  
		   	  String json = "{\"deviceId\":2089,\"sensorId\":5454,\"firstTime\":\""+time+"\"," +
		   	  		"\"lastTime\":\""+time+"\",\"data\":[{\"latitude\":"+location.getLatitude()+",\"longitude\":"+location.getLongitude()+",\"time\":\""+time+"\"}]}";
		   	    StringEntity se = new StringEntity(json);
		   	    //sets the post request as the resulting string
		   	    post.setEntity(se);
		   	    
		       post.setHeader("Authorization", "APIKey " + "ad2671ea-33f7-47a8-beed-d97824170f21");
		       post.setHeader("Content-Type", "application/octet-stream");
		  
		       HttpResponse response = client.execute(post);
		       Toast.makeText(LocationService.this,"sent",Toast.LENGTH_SHORT).show();
		       BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		       String line = "";
		       while ((line = rd.readLine()) != null) {
		         Log.d("gadgeteer",line);
		         Toast.makeText(LocationService.this,line,Toast.LENGTH_SHORT).show();
		       }
		     } catch (IOException e) {
		       e.printStackTrace();
		     }
		     //
		    } 
		}
      }

}
